var _ = require('lodash');
var Web3 = require('web3');


module.exports = (topBlock, client, cb) => {
  var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8546'));
  var startTime;

  var self = {
    time: 0,
    isActive: false
  };

  function connect() {
    if (web3.isConnected()) {
      onConnect();
    } else {
      setTimeout(connect, 1000);
    }
  }

  connect();

  function onConnect() {
    checkIfDone();

    web3.eth.isSyncing((err, sync) => {
      if (err) cb(err, self);

      if (sync && !startTime) {
        console.log('Starting sync');

        self.isActive = true;

        startTime = process.hrtime();
      }

      if (startTime) self.time = elapsedSince(startTime);

      checkIfDone();
    });
  }

  function checkIfDone() {
    var currentBlock = web3.eth.blockNumber;
    self.newBlocks = currentBlock - self.currentBlock;
    self.currentBlock = currentBlock;
    if (self.currentBlock >= topBlock) {
      self.isActive = false;
      cb(null, self);
    }
  }

  return self;
};

function elapsedSince(start) {
  var endTime = process.hrtime(start);

  return (endTime[0] + endTime[1] / 1000000000);
}
